package com.doge.controller.wx;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.base.BaseRespVo;
import com.doge.bean.bo.order.CommentBO;
import com.doge.bean.bo.order.OrderSubmitBO;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.pojo.order.OrderGoods;
import com.doge.bean.vo.order.OrderChangeStatusBO;
import com.doge.bean.vo.order.OrderForDetailVO;
import com.doge.bean.vo.order.OrderSubmitVO;
import com.doge.bean.vo.order.PrePayVO;
import com.doge.service.admin.order.OrderService;
import com.doge.service.wx.order.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStreamReader;

/**
 * @ClassName: OrderController
 * @Description: 订单管理
 * @author: PuJing
 * @create: 2021-12-02-10:24
 **/
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService WxorderService;


    /**
     * 查询指定条件的订单信息
     * http://182.92.235.201:8083/wx/order/list?
     * showType=0  &page=1&limit=10
     * showType=0 全部  1 待付款    2待发货   3 待收货  4 待评价
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam,Integer showType){

        BaseData data =  WxorderService.list(baseParam,showType);

        return BaseRespVo.ok(data);
    }

    /**
     * 提交订单
     * @param orderSubmitBO
     * @return
     */
    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody OrderSubmitBO orderSubmitBO){

        OrderSubmitVO orderSubmitVO = WxorderService.submit(orderSubmitBO);
        //return BaseRespVo.error(724,"订单支付失败(虽然其实成功了)");
        return BaseRespVo.ok(orderSubmitVO);
    }

    /**
     * 获得订单详情
     * @param orderId
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId){

        OrderForDetailVO orderForDetailVO = WxorderService.detail(orderId);

        return BaseRespVo.ok(orderForDetailVO);
    }


    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int orderStatus = 201;

        int code = WxorderService.orderChangeStatus(orderChangeStatusBO.getOrderId(),orderStatus);

       if(code == 200){
           // TODO 根据submit修改要返回的 BaseRespVo
           return BaseRespVo.ok(new PrePayVO());
       } else return BaseRespVo.error(724,"订单不能支付");
    }


    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int orderStatus = 102;

        int code = WxorderService.orderChangeStatus(orderChangeStatusBO.getOrderId(),orderStatus);

        if(code == 200){
            return BaseRespVo.ok();
        } else return BaseRespVo.error(724,"订单取消失败");
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int orderStatus = 401;

        int code = WxorderService.orderChangeConfirmStatus(orderChangeStatusBO.getOrderId(),orderStatus);

        if(code == 200){

            return BaseRespVo.ok();
        } else return BaseRespVo.error(724,"订单确认收货失败");
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int orderStatus = 202;

        int code = WxorderService.orderRefund(orderChangeStatusBO.getOrderId(),orderStatus);

        if(code == 200){
            return BaseRespVo.ok();
        } else return BaseRespVo.error(724,"订单确认收货失败");
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int code = WxorderService.delete(orderChangeStatusBO.getOrderId());

        if(code == 200){
            return BaseRespVo.ok();
        } else return BaseRespVo.error(724,"删除订单失败");
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId){

        OrderGoods orderGoods = WxorderService.goods(orderId,goodsId);

        return BaseRespVo.ok(orderGoods);
    }


    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody CommentBO commentBO){

        int code = WxorderService.comment(commentBO);

        if(code == 200){
            return BaseRespVo.ok();
        } else return BaseRespVo.error(724,"上传商品评价失败");

    }


}
